package ca.bcminecraft.utils;

import java.io.*;
import java.util.*;

/**
 *
 * @author Scott Stevens
 */
 
public class Configuration {
    
    private HashMap<Object, Object> config = new HashMap();
    private ArrayList<Object> file = new ArrayList();
    private List test;
    private String path;
    
    public Configuration (String path){
        try {
            // Set the global path.
            this.path = path;
            BufferedReader in = new BufferedReader (new FileReader (path));
            while (in.ready()){
                file.add(in.readLine());
            }
            in.close();
        } catch (FileNotFoundException e){
            System.err.println(e.getMessage());
        } catch (IOException e){
            System.err.println(e.getMessage());
        } finally {
            // Using secondary method to keep code clean.
            parseValues();
            file.clear();
        }
    }
    
    // Parsing method; appends file to local configuration.
    private void parseValues () {
        
        String[] values;
        
        if (!file.isEmpty()){
            for (int x = 0; x < file.size(); x++){
                if (((String)file.get(x)).startsWith("#") != true){
                    values = ((String)file.get(x)).split("=");
                    if (values[1].contains(",")){
                        ArrayList value = new ArrayList();
                        String[] parse = values[1].split(",");
                        value.addAll(Arrays.asList(parse));
                        config.put(values[0], value);
                    } else {
                        config.put(values[0], values[1]);
                    }
                }
            }
        }
    }
    
    public Object getKey (String key) {
        if (config.containsKey(key)){
            return config.get(key);
        } else {
            return null;
        }
    }
    
    public boolean addKey (String key, String value){
        if (!config.containsKey(key)){
            config.put(key, value);
            return true;
        } else {
            System.err.println("Key already exists.");
            
        }
        return false;
    }
    
    public boolean setKey (String key, String value){
        if (config.containsKey(key)){
            if (config.get(key) != value){
                config.remove(key);
                config.put(key, value);
                return true;
            }
        }
        
        System.err.println("Something went wrong.");
        return false;
    }
    
    public boolean removeKey(String key){
        if (config.containsKey(key)){
            config.remove(key);
            return true;
        }
        return false;
    }
    
    public void saveConfig () {
        // Not yet implemented. Will use custom .bcm filetype.
    }
    
    // Same as initial method; uses global path.
    public boolean reloadConfig () {
        try {
            file.clear();
            config.clear();
            BufferedReader in = new BufferedReader (new FileReader (path));
            while (in.ready()){
                file.add(in.readLine());
            }
            in.close();
        } catch (FileNotFoundException e){
            System.err.println(e.getMessage());
        } catch (IOException e){
            System.err.println(e.getMessage());
        } finally {
            parseValues();
            return true;
        }
        
        return false;
    }
    
}
